﻿using ContributionDeposit.CoreFrontEnd;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PagedList;
using System.Web.Mvc;
using Models.DataModel;

namespace ContributionDeposit.Controllers
{
    public class ContributionController : Core_FrontEnd
    {
        //
        // GET: /Contribution/
        public ActionResult Index(int? page, string searchtext = "")
        {
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(fun_db.GetListContribution(searchtext).ToPagedList(pageNumber, pageSize));
        }

        public JsonResult LoadData(int? page, string searchtext = "")
        {
            ViewBag.SearchText = searchtext;
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            var result = fun_db.GetListContribution(searchtext).ToPagedList(pageNumber, pageSize);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Contribution/Create
        public ActionResult Create()
        {
            ViewBag.ListCompany = new SelectList(fun_db.GetlstCompany().ToList(), "CompanyId", "Name");
            ViewBag.ListPort = new SelectList(fun_db.GetlstPort().ToList(), "PortId", "Name");
            ViewBag.ListPort1 = fun_db.GetlstPort().ToList();
            ViewBag.ListCompany1 = fun_db.GetlstCompany().ToList();
            return View();
        }

        //
        // POST: /Contribution/Create
        [HttpPost]
        public ActionResult Create(Contribution contribution)
        {
            UserAccount User = fun_db.GetObjUserByUsername(Session["user"].ToString());
            contribution.CreatedBy = User.UserAccountID;
            contribution.CreatedByName = User.UserName;
            contribution.ContributionId = Guid.NewGuid();
            contribution.CreatedOn = DateTime.Today;
            Company cmn=fun_db.GetCompany(contribution.CompanyId);
            contribution.CompanyName=cmn.Name;
            contribution.ModifiedOn = DateTime.Today;
            contribution.CompanyNumber=cmn.CompanyNumber;
            Port port=fun_db.GetObjPort(contribution.PortId);
            contribution.PortName=port.Name;
            contribution.PortNumber=port.PortNumber;
            try
            {
                // TODO: Add insert logic here
                fun_db.AddNewContribution(contribution);
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        /// <summary>
        /// suport for funtion autocomplete ajax in view
        /// </summary>
        /// <returns></returns>
        public JsonResult TextSearch()
        {
            List<Contribution> list = fun_db.GetListContribution().ToList();
            List<string> result = new List<string>();

            foreach (Contribution c in list)
            {
                result.Add(c.PortNumber);
                result.Add(c.PortName);
                result.Add(c.CompanyNumber);
                result.Add(c.CompanyName);
            }
            return Json(result.Distinct(), JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /Contribution/Delete/5
        public ActionResult Delete(Guid id)
        {
            var result = fun_db.DeleteContribution(id);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult DeleteSelectedContribution(IEnumerable<Guid> contributiondelete)
        {
            var result = fun_db.DeleteSelectedContribution(contributiondelete);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Details(Guid id)
        {
            return View(fun_db.GetContributionById(id));
        }

        public JsonResult GetListContributionFundByContribution(Guid id)
        {
            var result = fun_db.GetListContributionFundAllocation(id);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
